package com.dgshanger.sanhxiaofeisc.ui.person

import android.annotation.SuppressLint
import android.support.v4.content.ContextCompat
import android.view.View
import com.antiphon.paysdk.AntiphonPaySDK
import com.antiphon.paysdk.PayResultCallBack
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.PayForDetailEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.PayForDetailContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.PayForDetailPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.dgshanger.sanhxiaofeisc.ui.recharge.VipCardActivity
import com.xx.baseuilibrary.mvp.lcec.BaseMvpLcecActivity
import com.xxl.sodmall.dialog.WalletPayDialogWrapper
import kotlinx.android.synthetic.main.activity_pay_for_detail.*


/**
 * PayForDetailActivity
 * 沉迷学习不能自拔
 * Describe：代付详情
 * Created by 雷小星🍀 on 2018/4/19 15:47.
 */
class PayForDetailActivity : BaseMvpLcecActivity<View, PayForDetailEntity, PayForDetailContract.Model, PayForDetailContract.View, PayForDetailPresenter>(), PayForDetailContract.View {
    override fun setAccpteUI() {
        tv_reject.text = getString(R.string.Has_been_paid)
        tv_reject.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccent))
        tv_reject.visibility = View.VISIBLE
        view_choose.visibility = View.GONE
        view_pay.visibility = View.GONE
    }


    override fun getType(): String = mType!!

    override fun loadData(refresh: Boolean) {
        presenter.loadDetail()
    }

    override fun setRefuseUI() {
        tv_reject.text = getString(R.string.Rejected)
        tv_reject.visibility = View.VISIBLE
        view_choose.visibility = View.GONE
        view_pay.visibility = View.GONE
    }

    override fun createAliPay(info: String?) {
        AntiphonPaySDK.getIntance().createAliPay(info, object : PayResultCallBack {
            override fun onSuccess() {
                showToast(getString(R.string.pay_success))
                setAccpteUI()
            }

            override fun onFiale(p0: String?) {
                showToast(getString(R.string.Payment_error))
            }
        })
    }

    override fun createWeChatPay(info: String?) {
        AntiphonPaySDK.getIntance().createWxPay(info, object : PayResultCallBack {
            override fun onSuccess() {
                showToast(getString(R.string.pay_success))
                setAccpteUI()
            }

            override fun onFiale(p0: String?) {
                showToast(getString(R.string.Payment_error))
            }
        })
    }

    var mType: String? = null//代付类型,商家,个人
    private lateinit var extraID: String//代付类型,商家,个人

    private var entity: PayForDetailEntity? = null
    @SuppressLint("SetTextI18n")
    override fun setData(entity: PayForDetailEntity?) {
        showContent()
        this@PayForDetailActivity.entity = entity
        with(entity!!) {
            mType = msg_type
            extraID = extra_id
            tv_user_name.text = user_name
            tv_msg_content.text = msg_content
            tv_apply_pay_money.text = "¥" + apply_pay_money
            ImageLoader.loadAvatar(mContext, iv_user_avatar, entity.user_avatar)
            //若果已经拒绝则显示
            tv_reject.visibility = if (!"1".equals(apply_pay_status)) View.VISIBLE else View.GONE
            view_choose.visibility = if (!"1".equals(apply_pay_status)) View.GONE else View.VISIBLE
            view_wechat_pay.visibility = if (!PayForDetailEntity.PAY_FOR_PERSON.equals(msg_type)) View.GONE else View.VISIBLE
            view_ali_pay.visibility = if (!PayForDetailEntity.PAY_FOR_PERSON.equals(msg_type)) View.GONE else View.VISIBLE
            when (apply_pay_status) {
                "1" -> tv_reject.text = "待处理"
                "2" -> tv_reject.text = getString(R.string.expired)
                "3" -> tv_reject.text = getString(R.string.Rejected)
                "4" -> {
                    tv_reject.text = getString(R.string.Has_been_paid)
                    tv_reject.setTextColor(ContextCompat.getColor(mContext, R.color.colorAccent))
                }
            }
        }
    }

    override fun getMsgId(): String = intent.getStringExtra(Constants.KEY_INTENT_MSG_ID)

    override fun getExtraId(): String = extraID

    override fun createPresenter(): PayForDetailPresenter = PayForDetailPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_pay_for_detail

    override fun initData() {
        super.initData()
        AntiphonPaySDK.getIntance().init(this)
        loadData(true)
    }

    override fun initEvent() {
        iv_ok.setOnClickListener {
            //确认
            view_choose.visibility = View.GONE
            view_pay.visibility = View.VISIBLE
        }
        iv_cancel.setOnClickListener {
            //取消
            getPresenter().refuse()
        }

        view_wallet_pay.setOnClickListener {
            //零钱支付
            //判断用户是否有设置支付密码
            presenter.checkPayPassword()
        }
        view_ali_pay.setOnClickListener {
            //支付宝支付
            getPresenter().accept(VipCardActivity.PAY_ALIPAY, null)
        }
        view_wechat_pay.setOnClickListener {
            //微信支付
            getPresenter().accept(VipCardActivity.PAY_WX, null)
        }
    }

    override fun dismissPayDialog() {
        payDialogWrapper?.dismissDialog()
    }

    private var payDialogWrapper: WalletPayDialogWrapper? = null

    override fun showPayDialog() {
        payDialogWrapper = WalletPayDialogWrapper(mContext, entity!!.apply_pay_money)
                .setOnPasswordFinishListener(object : WalletPayDialogWrapper.OnPasswordFinishListener {
                    override fun onPasswordFinishListener(password: String) {
                        getPresenter().accept(VipCardActivity.PAY_WALLET, password)
                    }
                })
        payDialogWrapper?.showDialog()

    }

    companion object {

        val TYPE_PERSON = "1"//会员支付
        val TYPE_BUSINESS = "2"//商家支付
    }
}